package com.thirteenClassicsServer.common;

import com.thirteenClassics.cache.JdbSentenceCache;
import com.thirteenClassics.convert.EntryQueryConvertMapper;
import com.thirteenClassics.dto.*;
import com.thirteenClassics.entity.Book;
import com.thirteenClassics.entity.Catalog;
import com.thirteenClassics.entity.CatalogBook;
import com.thirteenClassics.entity.Sentence;
import com.thirteenClassics.exception.BaseException;
import com.thirteenClassics.vo.EntryCatalogQueryVO;
import com.thirteenClassics.vo.PictureVO;
import com.thirteenClassics.vo.SentenceVO;
import com.thirteenClassicsServer.cacheService.BookServiceCache;
import com.thirteenClassicsServer.cacheService.CatalogBookCacheService;
import com.thirteenClassicsServer.cacheService.JdbPicCache;
import com.thirteenClassicsServer.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.thirteenClassicsServer.service.impl.XmlParseService.toGetTableName;

@Service
public class EntryCommonService {

    @Autowired
    private CatalogBookMapper catalogBookMapper;
    @Autowired
    private CatalogMapper catalogMapper;
    @Resource
    private EntryMapper entryMapper;
    @Resource
    private BookMapper bookMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private SentenceMapper sentenceMapper;
    @Resource
    private JdbSentenceCache jdbSentenceCache;
    @Resource
    private PictureMapper pictureMapper;
    @Resource
    private SentenceLinkMapper sentenceLinkMapper;
    @Autowired
    private JdbPicCache jdbPicCache;
    @Resource
    private OperationHistoryMapper operationHistoryMapper;

    @Autowired
    private CommonService commonService;
    @Autowired
    private BookServiceCache bookServiceCache;
    @Autowired
    private CatalogBookCacheService catalogBookCacheService;
    @Autowired
    private EntryQueryConvertMapper entryQueryConvertMapper;


    public EntryCatalogQueryVO selectCatalogAndAllCommon(EntryCatalogQueryDTO entryCatalogQueryDTO) {
        EntryCatalogQueryVO entryCatalogQueryVO = new EntryCatalogQueryVO();

        Book book = bookServiceCache.selectBookById(entryCatalogQueryDTO.getBookId());

        // 设置书籍语言列表
        String lang = book.getBookLang();
        List<String> characterList = Arrays.asList(lang.trim().split(" "));
        entryCatalogQueryVO.setBookLang(new ArrayList<>(characterList));

        // 查询目录信息
        CatalogQueryDTO catalogQueryDTO = entryQueryConvertMapper.entryCatalogToCatalogDTO(entryCatalogQueryDTO);
        List<Catalog> list = catalogBookCacheService.selectCatalogOrdersByNameAndId(catalogQueryDTO);
        if (list.isEmpty() || catalogQueryDTO.getIndex() < 1 || catalogQueryDTO.getIndex() > list.size()) {
            throw new BaseException("目录信息不存在或索引越界");
        }

        Catalog catalog = list.get((int) (catalogQueryDTO.getIndex() - 1));
        CommonDTO commonDTO = new CommonDTO();
        commonDTO.setCatalogId(catalog.getId());//entryId
        //根据书id和catalogid查到在catalog_book中的记录
        CatalogBook catalogBook = catalogBookCacheService.getCatalogBookById(catalog.getId());
        if (catalogBook == null) {
            throw new BaseException("书在该章节内容不存在");
        }
        // 查询图片信息
        List<PictureVO> pictureVOS = jdbPicCache.getPicturesByCatalogBookId(catalogBook.getId());


        // 设置目录信息
        entryCatalogQueryVO.setId(catalogQueryDTO.getIndex());
        entryCatalogQueryVO.setName(catalog.getContents());
        entryCatalogQueryVO.setAncestors(catalog.getAncestors());
        entryCatalogQueryVO.setTot((long) list.size());
        entryCatalogQueryVO.setCatalogId(catalog.getId());
        entryCatalogQueryVO.setAlias(catalog.getAlias());
        entryCatalogQueryVO.setEntryId(catalogBook.getId());
        entryCatalogQueryVO.setImgs(pictureVOS);
        entryCatalogQueryVO.setImgsTot((long) pictureVOS.size());

        // 查询经典版句子信息
        if (!catalogBook.getCatalogIds().equals("0")) { // 该章节有对应经典版句子
            List<SentenceVO> sentenceVOS = jdbSentenceCache.jdbContentForChapter(JdbContentForChapterDTO.builder()
                    .bookId(entryCatalogQueryDTO.getBookId())
                    .bookName(entryCatalogQueryDTO.getBookName())
                    .index(entryCatalogQueryDTO.getIndex())
                    .build());
            entryCatalogQueryVO.setJdbContent(sentenceVOS);
        }

        SentenceSelectDYBDTO sentenceSelectDYBDTO3 = new SentenceSelectDYBDTO();
        sentenceSelectDYBDTO3.setEntryId(catalogBook.getId());
        sentenceSelectDYBDTO3.setSentenceIds(entryCatalogQueryDTO.getSentenceIndex());
        sentenceSelectDYBDTO3.setParagraphIds(entryCatalogQueryDTO.getParagraphIndex());
        //对照版的句子信息
        Sentence sentence3 = sentenceMapper.selectSentenceId(sentenceSelectDYBDTO3,
                toGetTableName(entryCatalogQueryDTO.getBookName()));

        List<Long> sentenceLinks = new ArrayList<>();

        if (sentence3 != null) {
            sentenceLinks = sentenceLinkMapper.selectSentenceId(sentence3.getSentenceId(),
                    entryCatalogQueryDTO.getBookId());

            entryCatalogQueryVO.setDybSentence(sentence3.getSentence());
            entryCatalogQueryVO.setSentence(sentence3.getSentence());
            entryCatalogQueryVO.setSentence2(sentence3.getSentence2());
            entryCatalogQueryVO.setDzSentence(sentence3.getSentenceDz());
            entryCatalogQueryVO.setManwengLd(sentence3.getManwengLd());
            entryCatalogQueryVO.setImgUrl(sentence3.getImgUrl());
        }
        entryCatalogQueryVO.setJdbLink(sentenceLinks);
        entryCatalogQueryVO.setJdb(sentenceLinks);

        return entryCatalogQueryVO;
    }

    public EntryCatalogQueryVO selectCatalogAndAllCommonNew(EntryCatalogQueryDTO entryCatalogQueryDTO) {
        EntryCatalogQueryVO entryCatalogQueryVO = new EntryCatalogQueryVO();

        Book book = bookServiceCache.selectBookById(entryCatalogQueryDTO.getBookId());

        // 设置书籍语言列表
        String lang = book.getBookLang();
        List<String> characterList = Arrays.asList(lang.trim().split(" "));
        entryCatalogQueryVO.setBookLang(new ArrayList<>(characterList));

        // 查询目录信息
        CatalogQueryDTO catalogQueryDTO = entryQueryConvertMapper.entryCatalogToCatalogDTO(entryCatalogQueryDTO);
        List<Catalog> list = catalogBookCacheService.selectCatalogOrdersByNameAndId(catalogQueryDTO);
        if (list.isEmpty() || catalogQueryDTO.getIndex() < 1 || catalogQueryDTO.getIndex() > list.size()) {
            throw new BaseException("目录信息不存在或索引越界");
        }

        Catalog catalog = list.get((int) (catalogQueryDTO.getIndex() - 1));
        CommonDTO commonDTO = new CommonDTO();
        commonDTO.setCatalogId(catalog.getId());//entryId
        //根据书id和catalogid查到在catalog_book中的记录
        CatalogBook catalogBook = catalogBookCacheService.getCatalogBookById(catalog.getId());
        if (catalogBook == null) {
            throw new BaseException("书在该章节内容不存在");
        }


        // 设置目录信息
        entryCatalogQueryVO.setId(catalogQueryDTO.getIndex());
        entryCatalogQueryVO.setName(catalog.getContents());
        entryCatalogQueryVO.setAncestors(catalog.getAncestors());
        entryCatalogQueryVO.setTot((long) list.size());
        entryCatalogQueryVO.setCatalogId(catalog.getId());
        entryCatalogQueryVO.setAlias(catalog.getAlias());
        entryCatalogQueryVO.setEntryId(catalogBook.getId());

        // 查询经典版句子信息
        if (!catalogBook.getCatalogIds().equals("0")) {

            SentenceSelectDYBDTO sentenceSelectDYBDTO3 = new SentenceSelectDYBDTO();
            sentenceSelectDYBDTO3.setEntryId(catalogBook.getId());
            sentenceSelectDYBDTO3.setSentenceIds(entryCatalogQueryDTO.getSentenceIndex());
            sentenceSelectDYBDTO3.setParagraphIds(entryCatalogQueryDTO.getParagraphIndex());
            //对照版的句子信息
            Sentence sentence3 = sentenceMapper.selectSentenceId(sentenceSelectDYBDTO3,
                    toGetTableName(entryCatalogQueryDTO.getBookName()));

            List<Long> sentenceLinks = new ArrayList<>();

            if (sentence3 != null) {
                sentenceLinks = sentenceLinkMapper.selectSentenceId(sentence3.getSentenceId(),
                        entryCatalogQueryDTO.getBookId());

                entryCatalogQueryVO.setDybSentence(sentence3.getSentence());
                entryCatalogQueryVO.setSentence(sentence3.getSentence());
                entryCatalogQueryVO.setSentence2(sentence3.getSentence2());
                entryCatalogQueryVO.setDzSentence(sentence3.getSentenceDz());
                entryCatalogQueryVO.setManwengLd(sentence3.getManwengLd());
                entryCatalogQueryVO.setImgUrl(sentence3.getImgUrl());
            }

            entryCatalogQueryVO.setJdbLink(sentenceLinks);
            entryCatalogQueryVO.setJdb(sentenceLinks);
        }
        return entryCatalogQueryVO;
    }
}

